import os
import matplotlib.pyplot as plt
import time

def benchmark(command):
    start_time = time.time()
    os.system(command)
    end_time = time.time()
    performance_metric = end_time - start_time
    return performance_metric

sizes = [256, 512, 1024, 2048, 4096]
cores = [1, 2, 4, 8]
blocks = [1, 2, 4, 8]
results = []

for size in sizes:
    performance_metrics = []
    for core in cores:
        print("Score: {size}, Core: {core}".format(size=size, core=core))
        blocks = size // core
        command = f'mpiexec -n {core} python3 main.py {size} {blocks}'
        performance_metric = benchmark(command)
        performance_metrics.append(performance_metric)
    results.append(performance_metrics)

# 生成图表
plt.figure(figsize=(10, 6))
for i, size in enumerate(sizes):
    plt.plot(cores, results[i], label=f"Matrix Size: {size}")
plt.xlabel('Number of Processes')
plt.ylabel('Execution Time (seconds)')
plt.legend()
plt.title('Performance Comparison for Different Matrix Sizes')
plt.show()